package net.kazed.nextaction.context.activity;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;

import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;
import net.kazed.nextaction.task.TaskView;

/**
 * ListAdapter for displaying list of Task objects. 
 *
 * @author Koert Zeilstra
 */
public class TaskListAdapter extends SimpleCursorAdapter {
   
   private static final String cTag = "TaskListAdapter";

   public TaskListAdapter(Context context, Cursor cursor) {
      super(context, R.layout.task_list_item_view, cursor, new String[] { Task.DESCRIPTION },
               new int[] { R.id.description });
   }
   
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      Log.d(cTag, "getView position=" + position + ". Old view=" + convertView);
      Cursor cursor = (Cursor) getItem(position);
      Task task = new Task(cursor);
      TaskView taskView;
      if (convertView instanceof TaskView) {
         taskView = (TaskView) convertView;
      } else {
         taskView = new TaskView(parent.getContext());
      }
      boolean isSelected = false;
      taskView.updateView(task, isSelected);
      return taskView;
   }

}
